<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="奖惩类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="奖惩类型" clearable style="width: 200px">
          <el-option label="奖励" :value="1" />
          <el-option label="惩罚" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="rewardPunishmentList" row-key="id">
      <el-table-column prop="userId" label="用户ID" width="120"></el-table-column>
      <el-table-column prop="type" label="奖惩类型" width="100">
        <template #default="scope">
          <span>{{ scope.row.type === 1 ? '奖励' : '惩罚' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="item" label="奖惩内容" width="300"></el-table-column>
      <el-table-column prop="description" label="详细描述" width="400"></el-table-column>
      <el-table-column prop="grade" label="级别" width="100"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改奖惩记录对话框 -->
    <el-dialog :title="dialogTitle" v-model="open" width="600px" append-to-body>
      <el-form ref="rewardPunishmentRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="奖惩类型" prop="type">
              <el-select v-model="form.type" placeholder="选择奖惩类型">
                <el-option label="奖励" value="1" />
                <el-option label="惩罚" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="奖惩内容" prop="item">
              <el-input v-model="form.item" placeholder="请输入奖惩内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细描述" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入详细描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="级别" prop="grade">
              <el-input v-model="form.grade" placeholder="请输入级别" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RewardPunishmentList">
import { ref, reactive } from 'vue';
import { listRewardPunishments, addRewardPunishment, updateRewardPunishment, deleteRewardPunishment } from "@/api/pages/rewardPunishment";

const loading = ref(true);
const open = ref(false);
const showSearch = ref(true);
const dialogTitle = ref("");
const refreshTable = ref(true);
const rewardPunishmentList = ref([]);
const form = reactive({
  id: undefined,
  userId: "",
  type: 1,
  item: "",
  description: "",
  grade: ""
});
const queryParams = reactive({
  userId: "",
  type: undefined
});
const rules = {
  userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }],
  item: [{ required: true, message: "奖惩内容不能为空", trigger: "blur" }],
  description: [{ required: true, message: "详细描述不能为空", trigger: "blur" }],
  grade: [{ required: true, message: "级别不能为空", trigger: "blur" }]
};

// 查询奖惩记录列表
const getList = () => {
  loading.value = true;
  listRewardPunishments(queryParams).then(response => {
    rewardPunishmentList.value = response.data;
    loading.value = false;
  });
};

// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};

// 重置表单
const reset = () => {
  Object.assign(form, {
    id: undefined,
    userId: "",
    type: 1,
    item: "",
    description: "",
    grade: ""
  });
};

// 搜索按钮操作
const handleQuery = () => {
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  Object.assign(queryParams, {
    userId: "",
    type: undefined
  });
  getList();
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  open.value = true;
  dialogTitle.value = "新增奖惩记录";
};

// 修改按钮操作
const handleUpdate = (row) => {
  Object.assign(form, row);
  open.value = true;
  dialogTitle.value = "修改奖惩记录";
};

// 提交按钮
const submitForm = () => {
  const apiCall = form.id ? updateRewardPunishment : addRewardPunishment;
  apiCall(form).then(() => {
    getList();
    open.value = false;
  });
};

// 删除按钮操作
const handleDelete = (row) => {
  deleteRewardPunishment(row.id).then(() => {
    getList();
  });
};

// 初始化奖惩记录列表
getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  text-align: right;
}
</style>